#!/bin/bash -e


CRTPKG=
TAR_OPTS=
TAR_OUT=
TMP_DIR=
SRC_DIR=
CRTLOG=
function 	initDir	{
	# $1 is the index to be printed
	# $2 is the file name
	
	cd $JAIL_PATH
	
	CRTPKG=$2
	TAR_OPTS=
	TAR_OUT=$JAIL_PATH/build-dir/$2
	SRC_DIR=$TAR_OUT
	TAR_IN=
	TMP_DIR=$JAIL_PATH/build-dir/$2-build
	CRTLOG=$JAIL_PATH/build-dir/$CRTPKG.log
	echo	"
		Target $CRTPKG initialised at $(date)
		
		
	" > $CRTLOG
	
	echo	""
	echo	""
	echo	"---------------------------------------------"
	echo	"  $1 $2"
	echo	"---------------------------------------------"
	echo	""
	
	# clear the directory where we will build the package
	if [ -d "$TMP_DIR" ]; then
		rm -rfv "$TMP_DIR" > /dev/null
	fi
	mkdir "$TMP_DIR"
	if [ -d "$SRC_DIR" ]; then
		rm -rfv "$SRC_DIR" > /dev/null
	fi
	
	# extract and transfer
	getCachedPackage $CRTPKG $SRC_DIR

	echo	"Sources were extracted. Building..."
	
	cd $TMP_DIR
}

function 	rDir	{
	rm -rfv $SRC_DIR >> $CRTLOG
	if [ ! -z "$TMP_DIR" ]; then
		rm -rfv $TMP_DIR >> $CRTLOG
	fi
}

function 	finishDir	{
	make >> $CRTLOG
	make install >> $CRTLOG
	rm -rfv $SRC_DIR >> $CRTLOG
	if [ ! -z "$TMP_DIR" ]; then
		rm -rfv $TMP_DIR >> $CRTLOG
	fi
}

function	doItAll		{
	# $1 is the index to be printed
	# $2 is the file name
	initDir $1 $2
	cd $SRC_DIR
	./configure --prefix=/tools
	finishDir
}

